<?php

namespace App\Http\Controllers\Admin;

use App;
use App\Common\Upload;
use App\Common\Status;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
use App\Http\Logic\Admin\UploadLogic;
use  App\Helper\Helper;
use Illuminate\Support\Facades\DB;

class UploadController extends BaseController
{
    /**
     * @param $type
     * @return \Illuminate\Http\JsonResponse
     */
    public function qnToken($type)
    {
        $token = Upload::getUploadToken((int)$type);

        return $this->success(Status::SUCCESS, "success", $token);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function upload(Request $request)
    {
        $path = $request->post();

        if (!isset($path['path']) || empty($path['path'])) {
            return $this->error(Status::ERROR, "缺少资源");
        }

        $id = Helper::createRes($path['path']);
        return $this->success(Status::SUCCESS, "success", ['id' => $id]);
    }


    public function getCity()
    {
        $data = DB::table('recruit_city')->select(["id", "name", "parentid", "leveltype"])
            ->where("leveltype",">=",2)->get();
        $tree = [];
        if ($data->isNotEmpty()) {
            $tree = $this->getcityTree($data->map(function ($value) {
                return (array)$value;
            })
                ->toArray());
        }
        return $this->success(Status::SUCCESS, "success", $tree);
    }


    /**
     * @param array $citys
     * @return array
     */
    private function getcityTree(array $citys)
    {
        $data = [];
        foreach ($citys as $city) {
            if ($city['leveltype'] === 2) {
                $data[] = $this->getChildren($citys, $city);
            }
        }
        return $data;
    }


    /**
     * @param $citys
     * @param $city
     * @return mixed
     */
    private function getChildren($citys, $city)
    {
        $c = [];
        foreach ($citys as $children) {
            if ($city['id'] === $children['parentid']) {
                $c[] = self::getChildren($citys, $children);
            }
        }
        $city['children'] = $c;
        return $city;
    }


    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function commonDt()
    {
        return $this->success(
            Status::SUCCESS,
            "success",
            [
                'business_type'  => Status::BUSINESS_TYPE,
                'business_scale' => Status::BUSINESS_SCALE,
                'education'      => Status::EDUCATION,
                'work_exp'       => Status::WORK_EXP,
                'job_type'       => Status::JOB_TYPE,
                'WORK_ST'        => Status::WORK_ST,
                'JOBS_SALARY'    => Status::JOBS_SALARY
            ]
        );
    }
}
